const Vue = require('vue')
const server = require('express')()
const renderer = require('vue-server-renderer').createRenderer()

const renderer = createRenderer({
	template: require('fs').readFileSync('./index.template.html', 'utf-8'),
})

server.get('*', (req, res) => {
	const app = new Vue({
		data: {
			url: req.url
		},
		template: `<div>now url: {{ url }}</div>`
	})

	// 声明一个变量用作渲染模板的数据
	const context = {
		title: 'HELLO',
		meta: `
			<meta charset="utf-8">
		`
	}

	renderer.renderTostring(app, context, (err, html) => {
		if (err) {
			res.status(500).end('Internal Server Error-' + err.message)
			return
		}
		res.end(html)
		console.log(html)
		// html 将是注入应用程序内容的完整叶念
	})

})

server.listen(8080)